<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">


<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    
    <title>pgmpy.models.MarkovModel &#8212; pgmpy 0.1.2 documentation</title>
    
    <link rel="stylesheet" href="../../../_static/sphinxdoc.css" type="text/css" />
    <link rel="stylesheet" href="../../../_static/pygments.css" type="text/css" />
    
    <script type="text/javascript">
      var DOCUMENTATION_OPTIONS = {
        URL_ROOT:    '../../../',
        VERSION:     '0.1.2',
        COLLAPSE_INDEX: false,
        FILE_SUFFIX: '.html',
        HAS_SOURCE:  true,
        SOURCELINK_SUFFIX: '.txt'
      };
    </script>
    <script type="text/javascript" src="../../../_static/jquery.js"></script>
    <script type="text/javascript" src="../../../_static/underscore.js"></script>
    <script type="text/javascript" src="../../../_static/doctools.js"></script>
    <link rel="index" title="Index" href="../../../genindex.html" />
    <link rel="search" title="Search" href="../../../search.html" /> 
  </head>
  <body role="document">
    <div class="related" role="navigation" aria-label="related navigation">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../../../genindex.html" title="General Index"
             accesskey="I">index</a></li>
        <li class="right" >
          <a href="../../../py-modindex.html" title="Python Module Index"
             >modules</a> |</li>
        <li class="nav-item nav-item-0"><a href="../../../index.html">pgmpy 0.1.2 documentation</a> &#187;</li>
          <li class="nav-item nav-item-1"><a href="../../index.html" accesskey="U">Module code</a> &#187;</li> 
      </ul>
    </div>
      <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
        <div class="sphinxsidebarwrapper">
            <p class="logo"><a href="../../../index.html">
              <img class="logo" src="../../../_static/logo.png" alt="Logo"/>
            </a></p>
<div id="searchbox" style="display: none" role="search">
  <h3>Quick search</h3>
    <form class="search" action="../../../search.html" method="get">
      <div><input type="text" name="q" /></div>
      <div><input type="submit" value="Go" /></div>
      <input type="hidden" name="check_keywords" value="yes" />
      <input type="hidden" name="area" value="default" />
    </form>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
        </div>
      </div>

    <div class="document">
      <div class="documentwrapper">
        <div class="bodywrapper">
          <div class="body" role="main">
            
  <h1>Source code for pgmpy.models.MarkovModel</h1><div class="highlight"><pre>
<span></span><span class="ch">#!/usr/bin/env python3</span>
<span class="kn">import</span> <span class="nn">itertools</span>
<span class="kn">from</span> <span class="nn">collections</span> <span class="k">import</span> <span class="n">defaultdict</span>

<span class="kn">import</span> <span class="nn">networkx</span> <span class="k">as</span> <span class="nn">nx</span>
<span class="kn">import</span> <span class="nn">numpy</span> <span class="k">as</span> <span class="nn">np</span>

<span class="kn">from</span> <span class="nn">pgmpy.base</span> <span class="k">import</span> <span class="n">UndirectedGraph</span>
<span class="kn">from</span> <span class="nn">pgmpy.factors.discrete</span> <span class="k">import</span> <span class="n">DiscreteFactor</span>
<span class="kn">from</span> <span class="nn">pgmpy.factors</span> <span class="k">import</span> <span class="n">factor_product</span>
<span class="kn">from</span> <span class="nn">pgmpy.independencies</span> <span class="k">import</span> <span class="n">Independencies</span>
<span class="kn">from</span> <span class="nn">pgmpy.extern.six.moves</span> <span class="k">import</span> <span class="nb">map</span><span class="p">,</span> <span class="nb">range</span><span class="p">,</span> <span class="nb">zip</span>


<div class="viewcode-block" id="MarkovModel"><a class="viewcode-back" href="../../../models.html#pgmpy.models.MarkovModel.MarkovModel">[docs]</a><span class="k">class</span> <span class="nc">MarkovModel</span><span class="p">(</span><span class="n">UndirectedGraph</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Base class for markov model.</span>

<span class="sd">    A MarkovModel stores nodes and edges with potentials</span>

<span class="sd">    MarkovModel holds undirected edges.</span>

<span class="sd">    Parameters</span>
<span class="sd">    ----------</span>
<span class="sd">    data : input graph</span>
<span class="sd">        Data to initialize graph.  If data=None (default) an empty</span>
<span class="sd">        graph is created.  The data can be an edge list, or any</span>
<span class="sd">        NetworkX graph object.</span>

<span class="sd">    Examples</span>
<span class="sd">    --------</span>
<span class="sd">    Create an empty Markov Model with no nodes and no edges.</span>

<span class="sd">    &gt;&gt;&gt; from pgmpy.models import MarkovModel</span>
<span class="sd">    &gt;&gt;&gt; G = MarkovModel()</span>

<span class="sd">    G can be grown in several ways.</span>

<span class="sd">    **Nodes:**</span>

<span class="sd">    Add one node at a time:</span>

<span class="sd">    &gt;&gt;&gt; G.add_node(&#39;a&#39;)</span>

<span class="sd">    Add the nodes from any container (a list, set or tuple or the nodes</span>
<span class="sd">    from another graph).</span>

<span class="sd">    &gt;&gt;&gt; G.add_nodes_from([&#39;a&#39;, &#39;b&#39;])</span>

<span class="sd">    **Edges:**</span>

<span class="sd">    G can also be grown by adding edges.</span>

<span class="sd">    Add one edge,</span>

<span class="sd">    &gt;&gt;&gt; G.add_edge(&#39;a&#39;, &#39;b&#39;)</span>

<span class="sd">    a list of edges,</span>

<span class="sd">    &gt;&gt;&gt; G.add_edges_from([(&#39;a&#39;, &#39;b&#39;), (&#39;b&#39;, &#39;c&#39;)])</span>

<span class="sd">    If some edges connect nodes not yet in the model, the nodes</span>
<span class="sd">    are added automatically.  There are no errors when adding</span>
<span class="sd">    nodes or edges that already exist.</span>

<span class="sd">    **Shortcuts:**</span>

<span class="sd">    Many common graph features allow python syntax for speed reporting.</span>

<span class="sd">    &gt;&gt;&gt; &#39;a&#39; in G     # check if node in graph</span>
<span class="sd">    True</span>
<span class="sd">    &gt;&gt;&gt; len(G)  # number of nodes in graph</span>
<span class="sd">    3</span>

<span class="sd">    Public Methods</span>
<span class="sd">    --------------</span>
<span class="sd">    add_node(&#39;node1&#39;)</span>
<span class="sd">    add_nodes_from([&#39;node1&#39;, &#39;node2&#39;, ...])</span>
<span class="sd">    add_edge(&#39;node1&#39;, &#39;node2&#39;)</span>
<span class="sd">    add_edges_from([(&#39;node1&#39;, &#39;node2&#39;),(&#39;node3&#39;, &#39;node4&#39;)])</span>
<span class="sd">    &quot;&quot;&quot;</span>

    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">ebunch</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
        <span class="nb">super</span><span class="p">(</span><span class="n">MarkovModel</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="n">__init__</span><span class="p">()</span>
        <span class="k">if</span> <span class="n">ebunch</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">add_edges_from</span><span class="p">(</span><span class="n">ebunch</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">factors</span> <span class="o">=</span> <span class="p">[]</span>

<div class="viewcode-block" id="MarkovModel.add_edge"><a class="viewcode-back" href="../../../models.html#pgmpy.models.MarkovModel.MarkovModel.add_edge">[docs]</a>    <span class="k">def</span> <span class="nf">add_edge</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">u</span><span class="p">,</span> <span class="n">v</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Add an edge between u and v.</span>

<span class="sd">        The nodes u and v will be automatically added if they are</span>
<span class="sd">        not already in the graph</span>

<span class="sd">        Parameters</span>
<span class="sd">        ----------</span>
<span class="sd">        u,v : nodes</span>
<span class="sd">            Nodes can be any hashable Python object.</span>

<span class="sd">        Examples</span>
<span class="sd">        --------</span>
<span class="sd">        &gt;&gt;&gt; from pgmpy.models import MarkovModel</span>
<span class="sd">        &gt;&gt;&gt; G = MarkovModel()</span>
<span class="sd">        &gt;&gt;&gt; G.add_nodes_from([&#39;Alice&#39;, &#39;Bob&#39;, &#39;Charles&#39;])</span>
<span class="sd">        &gt;&gt;&gt; G.add_edge(&#39;Alice&#39;, &#39;Bob&#39;)</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="c1"># check that there is no self loop.</span>
        <span class="k">if</span> <span class="n">u</span> <span class="o">!=</span> <span class="n">v</span><span class="p">:</span>
            <span class="nb">super</span><span class="p">(</span><span class="n">MarkovModel</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="n">add_edge</span><span class="p">(</span><span class="n">u</span><span class="p">,</span> <span class="n">v</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">)</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s1">&#39;Self loops are not allowed&#39;</span><span class="p">)</span></div>

<div class="viewcode-block" id="MarkovModel.add_factors"><a class="viewcode-back" href="../../../models.html#pgmpy.models.MarkovModel.MarkovModel.add_factors">[docs]</a>    <span class="k">def</span> <span class="nf">add_factors</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="o">*</span><span class="n">factors</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Associate a factor to the graph.</span>
<span class="sd">        See factors class for the order of potential values</span>

<span class="sd">        Parameters</span>
<span class="sd">        ----------</span>
<span class="sd">        *factor: pgmpy.factors.factors object</span>
<span class="sd">            A factor object on any subset of the variables of the model which</span>
<span class="sd">            is to be associated with the model.</span>

<span class="sd">        Returns</span>
<span class="sd">        -------</span>
<span class="sd">        None</span>

<span class="sd">        Examples</span>
<span class="sd">        --------</span>
<span class="sd">        &gt;&gt;&gt; from pgmpy.models import MarkovModel</span>
<span class="sd">        &gt;&gt;&gt; from pgmpy.factors.discrete import DiscreteFactor</span>
<span class="sd">        &gt;&gt;&gt; student = MarkovModel([(&#39;Alice&#39;, &#39;Bob&#39;), (&#39;Bob&#39;, &#39;Charles&#39;),</span>
<span class="sd">        ...                        (&#39;Charles&#39;, &#39;Debbie&#39;), (&#39;Debbie&#39;, &#39;Alice&#39;)])</span>
<span class="sd">        &gt;&gt;&gt; factor = DiscreteFactor([&#39;Alice&#39;, &#39;Bob&#39;], cardinality=[3, 2],</span>
<span class="sd">        ...                 values=np.random.rand(6))</span>
<span class="sd">        &gt;&gt;&gt; student.add_factors(factor)</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">for</span> <span class="n">factor</span> <span class="ow">in</span> <span class="n">factors</span><span class="p">:</span>
            <span class="k">if</span> <span class="nb">set</span><span class="p">(</span><span class="n">factor</span><span class="o">.</span><span class="n">variables</span><span class="p">)</span> <span class="o">-</span> <span class="nb">set</span><span class="p">(</span><span class="n">factor</span><span class="o">.</span><span class="n">variables</span><span class="p">)</span><span class="o">.</span><span class="n">intersection</span><span class="p">(</span>
                    <span class="nb">set</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">nodes</span><span class="p">())):</span>
                <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s2">&quot;Factors defined on variable not in the model&quot;</span><span class="p">,</span>
                                 <span class="n">factor</span><span class="p">)</span>

            <span class="bp">self</span><span class="o">.</span><span class="n">factors</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">factor</span><span class="p">)</span></div>

<div class="viewcode-block" id="MarkovModel.get_factors"><a class="viewcode-back" href="../../../models.html#pgmpy.models.MarkovModel.MarkovModel.get_factors">[docs]</a>    <span class="k">def</span> <span class="nf">get_factors</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">node</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Returns all the factors containing the node. If node is not specified</span>
<span class="sd">        returns all the factors that have been added till now to the graph.</span>

<span class="sd">        Parameter</span>
<span class="sd">        ---------</span>
<span class="sd">        node: any hashable python object (optional)</span>
<span class="sd">            The node whose factor we want. If node is not specified</span>

<span class="sd">        Examples</span>
<span class="sd">        --------</span>
<span class="sd">        &gt;&gt;&gt; from pgmpy.models import MarkovModel</span>
<span class="sd">        &gt;&gt;&gt; from pgmpy.factors.discrete import DiscreteFactor</span>
<span class="sd">        &gt;&gt;&gt; student = MarkovModel([(&#39;Alice&#39;, &#39;Bob&#39;), (&#39;Bob&#39;, &#39;Charles&#39;)])</span>
<span class="sd">        &gt;&gt;&gt; factor1 = DiscreteFactor([&#39;Alice&#39;, &#39;Bob&#39;], cardinality=[2, 2],</span>
<span class="sd">        ...                 values=np.random.rand(4))</span>
<span class="sd">        &gt;&gt;&gt; factor2 = DiscreteFactor([&#39;Bob&#39;, &#39;Charles&#39;], cardinality=[2, 3],</span>
<span class="sd">        ...					values=np.ones(6))</span>
<span class="sd">        &gt;&gt;&gt; student.add_factors(factor1,factor2)</span>
<span class="sd">        &gt;&gt;&gt; student.get_factors()</span>
<span class="sd">        [&lt;DiscreteFactor representing phi(Alice:2, Bob:2) at 0x7f8a0e9bf630&gt;,</span>
<span class="sd">         &lt;DiscreteFactor representing phi(Bob:2, Charles:3) at 0x7f8a0e9bf5f8&gt;]</span>
<span class="sd">        &gt;&gt;&gt; student.get_factors(&#39;Alice&#39;)</span>
<span class="sd">        [&lt;DiscreteFactor representing phi(Alice:2, Bob:2) at 0x7f8a0e9bf630&gt;]</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">if</span> <span class="n">node</span><span class="p">:</span>
            <span class="k">if</span> <span class="n">node</span> <span class="ow">not</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">nodes</span><span class="p">():</span>
                <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s1">&#39;Node not present in the Undirected Graph&#39;</span><span class="p">)</span>
            <span class="n">node_factors</span> <span class="o">=</span> <span class="p">[]</span>
            <span class="k">for</span> <span class="n">factor</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">factors</span><span class="p">:</span>
                <span class="k">if</span> <span class="n">node</span> <span class="ow">in</span> <span class="n">factor</span><span class="o">.</span><span class="n">scope</span><span class="p">():</span>
                    <span class="n">node_factors</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">factor</span><span class="p">)</span>
            <span class="k">return</span> <span class="n">node_factors</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">factors</span></div>


<div class="viewcode-block" id="MarkovModel.remove_factors"><a class="viewcode-back" href="../../../models.html#pgmpy.models.MarkovModel.MarkovModel.remove_factors">[docs]</a>    <span class="k">def</span> <span class="nf">remove_factors</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="o">*</span><span class="n">factors</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Removes the given factors from the added factors.</span>

<span class="sd">        Examples</span>
<span class="sd">        --------</span>
<span class="sd">        &gt;&gt;&gt; from pgmpy.models import MarkovModel</span>
<span class="sd">        &gt;&gt;&gt; from pgmpy.factors.discrete import DiscreteFactor</span>
<span class="sd">        &gt;&gt;&gt; student = MarkovModel([(&#39;Alice&#39;, &#39;Bob&#39;), (&#39;Bob&#39;, &#39;Charles&#39;)])</span>
<span class="sd">        &gt;&gt;&gt; factor = DiscreteFactor([&#39;Alice&#39;, &#39;Bob&#39;], cardinality=[2, 2],</span>
<span class="sd">        ...                 values=np.random.rand(4))</span>
<span class="sd">        &gt;&gt;&gt; student.add_factors(factor)</span>
<span class="sd">        &gt;&gt;&gt; student.remove_factors(factor)</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">for</span> <span class="n">factor</span> <span class="ow">in</span> <span class="n">factors</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">factors</span><span class="o">.</span><span class="n">remove</span><span class="p">(</span><span class="n">factor</span><span class="p">)</span></div>

<div class="viewcode-block" id="MarkovModel.get_cardinality"><a class="viewcode-back" href="../../../models.html#pgmpy.models.MarkovModel.MarkovModel.get_cardinality">[docs]</a>    <span class="k">def</span> <span class="nf">get_cardinality</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">node</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Returns the cardinality of the node. If node is not specified returns</span>
<span class="sd">        a dictionary with the given variable as keys and their respective cardinality</span>
<span class="sd">        as values.</span>

<span class="sd">        Parameters</span>
<span class="sd">        ----------</span>
<span class="sd">        node: any hashable python object (optional)</span>
<span class="sd">            The node whose cardinality we want. If node is not specified returns a</span>
<span class="sd">            dictionary with the given variable as keys and their respective cardinality</span>
<span class="sd">            as values.</span>

<span class="sd">        Examples</span>
<span class="sd">        --------</span>
<span class="sd">        &gt;&gt;&gt; from pgmpy.models import MarkovModel</span>
<span class="sd">        &gt;&gt;&gt; from pgmpy.factors.discrete import DiscreteFactor</span>
<span class="sd">        &gt;&gt;&gt; student = MarkovModel([(&#39;Alice&#39;, &#39;Bob&#39;), (&#39;Bob&#39;, &#39;Charles&#39;)])</span>
<span class="sd">        &gt;&gt;&gt; factor = DiscreteFactor([&#39;Alice&#39;, &#39;Bob&#39;], cardinality=[2, 2],</span>
<span class="sd">        ...                 values=np.random.rand(4))</span>
<span class="sd">        &gt;&gt;&gt; student.add_factors(factor)</span>
<span class="sd">        &gt;&gt;&gt; student.get_cardinality(node=&#39;Alice&#39;)</span>
<span class="sd">        2</span>
<span class="sd">        &gt;&gt;&gt; student.get_cardinality()</span>
<span class="sd">        defaultdict(&lt;class &#39;int&#39;&gt;, {&#39;Bob&#39;: 2, &#39;Alice&#39;: 2})</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">if</span> <span class="n">node</span><span class="p">:</span>
            <span class="k">for</span> <span class="n">factor</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">factors</span><span class="p">:</span>
                <span class="k">for</span> <span class="n">variable</span><span class="p">,</span> <span class="n">cardinality</span> <span class="ow">in</span> <span class="nb">zip</span><span class="p">(</span><span class="n">factor</span><span class="o">.</span><span class="n">scope</span><span class="p">(),</span> <span class="n">factor</span><span class="o">.</span><span class="n">cardinality</span><span class="p">):</span>
                    <span class="k">if</span> <span class="n">node</span> <span class="o">==</span> <span class="n">variable</span><span class="p">:</span>
                        <span class="k">return</span> <span class="n">cardinality</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">cardinalities</span> <span class="o">=</span> <span class="n">defaultdict</span><span class="p">(</span><span class="nb">int</span><span class="p">)</span>
            <span class="k">for</span> <span class="n">factor</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">factors</span><span class="p">:</span>
                    <span class="k">for</span> <span class="n">variable</span><span class="p">,</span> <span class="n">cardinality</span> <span class="ow">in</span> <span class="nb">zip</span><span class="p">(</span><span class="n">factor</span><span class="o">.</span><span class="n">scope</span><span class="p">(),</span> <span class="n">factor</span><span class="o">.</span><span class="n">cardinality</span><span class="p">):</span>
                        <span class="n">cardinalities</span><span class="p">[</span><span class="n">variable</span><span class="p">]</span> <span class="o">=</span> <span class="n">cardinality</span>
            <span class="k">return</span> <span class="n">cardinalities</span></div>

<div class="viewcode-block" id="MarkovModel.check_model"><a class="viewcode-back" href="../../../models.html#pgmpy.models.MarkovModel.MarkovModel.check_model">[docs]</a>    <span class="k">def</span> <span class="nf">check_model</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Check the model for various errors. This method checks for the following</span>
<span class="sd">        errors -</span>

<span class="sd">        * Checks if the cardinalities of all the variables are consistent across all the factors.</span>
<span class="sd">        * Factors are defined for all the random variables.</span>

<span class="sd">        Returns</span>
<span class="sd">        -------</span>
<span class="sd">        check: boolean</span>
<span class="sd">            True if all the checks are passed</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">cardinalities</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_cardinality</span><span class="p">()</span>
        <span class="k">for</span> <span class="n">factor</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">factors</span><span class="p">:</span>
            <span class="k">for</span> <span class="n">variable</span><span class="p">,</span> <span class="n">cardinality</span> <span class="ow">in</span> <span class="nb">zip</span><span class="p">(</span><span class="n">factor</span><span class="o">.</span><span class="n">scope</span><span class="p">(),</span> <span class="n">factor</span><span class="o">.</span><span class="n">cardinality</span><span class="p">):</span>
                <span class="k">if</span> <span class="n">cardinalities</span><span class="p">[</span><span class="n">variable</span><span class="p">]</span> <span class="o">!=</span> <span class="n">cardinality</span><span class="p">:</span>
                    <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span>
                        <span class="s1">&#39;Cardinality of variable </span><span class="si">{var}</span><span class="s1"> not matching among factors&#39;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">var</span><span class="o">=</span><span class="n">variable</span><span class="p">))</span>
                <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">nodes</span><span class="p">())</span> <span class="o">!=</span> <span class="nb">len</span><span class="p">(</span><span class="n">cardinalities</span><span class="p">):</span>
                    <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s1">&#39;Factors for all the variables not defined&#39;</span><span class="p">)</span>
            <span class="k">for</span> <span class="n">var1</span><span class="p">,</span> <span class="n">var2</span> <span class="ow">in</span> <span class="n">itertools</span><span class="o">.</span><span class="n">combinations</span><span class="p">(</span><span class="n">factor</span><span class="o">.</span><span class="n">variables</span><span class="p">,</span> <span class="mi">2</span><span class="p">):</span>
                <span class="k">if</span> <span class="n">var2</span> <span class="ow">not</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">neighbors</span><span class="p">(</span><span class="n">var1</span><span class="p">):</span>
                    <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s2">&quot;DiscreteFactor inconsistent with the model.&quot;</span><span class="p">)</span>
        <span class="k">return</span> <span class="kc">True</span></div>

<div class="viewcode-block" id="MarkovModel.to_factor_graph"><a class="viewcode-back" href="../../../models.html#pgmpy.models.MarkovModel.MarkovModel.to_factor_graph">[docs]</a>    <span class="k">def</span> <span class="nf">to_factor_graph</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Converts the markov model into factor graph.</span>

<span class="sd">        A factor graph contains two types of nodes. One type corresponds to</span>
<span class="sd">        random variables whereas the second type corresponds to factors over</span>
<span class="sd">        these variables. The graph only contains edges between variables and</span>
<span class="sd">        factor nodes. Each factor node is associated with one factor whose</span>
<span class="sd">        scope is the set of variables that are its neighbors.</span>

<span class="sd">        Examples</span>
<span class="sd">        --------</span>
<span class="sd">        &gt;&gt;&gt; from pgmpy.models import MarkovModel</span>
<span class="sd">        &gt;&gt;&gt; from pgmpy.factors.discrete import DiscreteFactor</span>
<span class="sd">        &gt;&gt;&gt; student = MarkovModel([(&#39;Alice&#39;, &#39;Bob&#39;), (&#39;Bob&#39;, &#39;Charles&#39;)])</span>
<span class="sd">        &gt;&gt;&gt; factor1 = DiscreteFactor([&#39;Alice&#39;, &#39;Bob&#39;], [3, 2], np.random.rand(6))</span>
<span class="sd">        &gt;&gt;&gt; factor2 = DiscreteFactor([&#39;Bob&#39;, &#39;Charles&#39;], [2, 2], np.random.rand(4))</span>
<span class="sd">        &gt;&gt;&gt; student.add_factors(factor1, factor2)</span>
<span class="sd">        &gt;&gt;&gt; factor_graph = student.to_factor_graph()</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="kn">from</span> <span class="nn">pgmpy.models</span> <span class="k">import</span> <span class="n">FactorGraph</span>
        <span class="n">factor_graph</span> <span class="o">=</span> <span class="n">FactorGraph</span><span class="p">()</span>

        <span class="k">if</span> <span class="ow">not</span> <span class="bp">self</span><span class="o">.</span><span class="n">factors</span><span class="p">:</span>
            <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s1">&#39;Factors not associated with the random variables.&#39;</span><span class="p">)</span>

        <span class="n">factor_graph</span><span class="o">.</span><span class="n">add_nodes_from</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">nodes</span><span class="p">())</span>
        <span class="k">for</span> <span class="n">factor</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">factors</span><span class="p">:</span>
            <span class="n">scope</span> <span class="o">=</span> <span class="n">factor</span><span class="o">.</span><span class="n">scope</span><span class="p">()</span>
            <span class="n">factor_node</span> <span class="o">=</span> <span class="s1">&#39;phi_&#39;</span> <span class="o">+</span> <span class="s1">&#39;_&#39;</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">scope</span><span class="p">)</span>
            <span class="n">factor_graph</span><span class="o">.</span><span class="n">add_edges_from</span><span class="p">(</span><span class="n">itertools</span><span class="o">.</span><span class="n">product</span><span class="p">(</span><span class="n">scope</span><span class="p">,</span> <span class="p">[</span><span class="n">factor_node</span><span class="p">]))</span>
            <span class="n">factor_graph</span><span class="o">.</span><span class="n">add_factors</span><span class="p">(</span><span class="n">factor</span><span class="p">)</span>

        <span class="k">return</span> <span class="n">factor_graph</span></div>

<div class="viewcode-block" id="MarkovModel.triangulate"><a class="viewcode-back" href="../../../models.html#pgmpy.models.MarkovModel.MarkovModel.triangulate">[docs]</a>    <span class="k">def</span> <span class="nf">triangulate</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">heuristic</span><span class="o">=</span><span class="s1">&#39;H6&#39;</span><span class="p">,</span> <span class="n">order</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">inplace</span><span class="o">=</span><span class="kc">False</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Triangulate the graph.</span>

<span class="sd">        If order of deletion is given heuristic algorithm will not be used.</span>

<span class="sd">        Parameters</span>
<span class="sd">        ----------</span>
<span class="sd">        heuristic: H1 | H2 | H3 | H4 | H5 | H6</span>
<span class="sd">            The heuristic algorithm to use to decide the deletion order of</span>
<span class="sd">            the variables to compute the triangulated graph.</span>
<span class="sd">            Let X be the set of variables and X(i) denotes the i-th variable.</span>

<span class="sd">            * S(i) - The size of the clique created by deleting the variable.</span>
<span class="sd">            * E(i) - Cardinality of variable X(i).</span>
<span class="sd">            * M(i) - Maximum size of cliques given by X(i) and its adjacent nodes.</span>
<span class="sd">            * C(i) - Sum of size of cliques given by X(i) and its adjacent nodes.</span>

<span class="sd">            The heuristic algorithm decide the deletion order if this way:</span>

<span class="sd">            * H1 - Delete the variable with minimal S(i).</span>
<span class="sd">            * H2 - Delete the variable with minimal S(i)/E(i).</span>
<span class="sd">            * H3 - Delete the variable with minimal S(i) - M(i).</span>
<span class="sd">            * H4 - Delete the variable with minimal S(i) - C(i).</span>
<span class="sd">            * H5 - Delete the variable with minimal S(i)/M(i).</span>
<span class="sd">            * H6 - Delete the variable with minimal S(i)/C(i).</span>

<span class="sd">        order: list, tuple (array-like)</span>
<span class="sd">            The order of deletion of the variables to compute the triagulated</span>
<span class="sd">            graph. If order is given heuristic algorithm will not be used.</span>

<span class="sd">        inplace: True | False</span>
<span class="sd">            if inplace is true then adds the edges to the object from</span>
<span class="sd">            which it is called else returns a new object.</span>

<span class="sd">        Reference</span>
<span class="sd">        ---------</span>
<span class="sd">        http://citeseerx.ist.psu.edu/viewdoc/summary?doi=10.1.1.56.3607</span>

<span class="sd">        Examples</span>
<span class="sd">        --------</span>
<span class="sd">        &gt;&gt;&gt; from pgmpy.models import MarkovModel</span>
<span class="sd">        &gt;&gt;&gt; from pgmpy.factors.discrete import DiscreteFactor</span>
<span class="sd">        &gt;&gt;&gt; G = MarkovModel()</span>
<span class="sd">        &gt;&gt;&gt; G.add_nodes_from([&#39;x1&#39;, &#39;x2&#39;, &#39;x3&#39;, &#39;x4&#39;, &#39;x5&#39;, &#39;x6&#39;, &#39;x7&#39;])</span>
<span class="sd">        &gt;&gt;&gt; G.add_edges_from([(&#39;x1&#39;, &#39;x3&#39;), (&#39;x1&#39;, &#39;x4&#39;), (&#39;x2&#39;, &#39;x4&#39;),</span>
<span class="sd">        ...                   (&#39;x2&#39;, &#39;x5&#39;), (&#39;x3&#39;, &#39;x6&#39;), (&#39;x4&#39;, &#39;x6&#39;),</span>
<span class="sd">        ...                   (&#39;x4&#39;, &#39;x7&#39;), (&#39;x5&#39;, &#39;x7&#39;)])</span>
<span class="sd">        &gt;&gt;&gt; phi = [DiscreteFactor(edge, [2, 2], np.random.rand(4)) for edge in G.edges()]</span>
<span class="sd">        &gt;&gt;&gt; G.add_factors(*phi)</span>
<span class="sd">        &gt;&gt;&gt; G_chordal = G.triangulate()</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">check_model</span><span class="p">()</span>

        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">is_triangulated</span><span class="p">():</span>
            <span class="k">if</span> <span class="n">inplace</span><span class="p">:</span>
                <span class="k">return</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="k">return</span> <span class="bp">self</span>

        <span class="n">graph_copy</span> <span class="o">=</span> <span class="n">nx</span><span class="o">.</span><span class="n">Graph</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">edges</span><span class="p">())</span>
        <span class="n">edge_set</span> <span class="o">=</span> <span class="nb">set</span><span class="p">()</span>

        <span class="k">def</span> <span class="nf">_find_common_cliques</span><span class="p">(</span><span class="n">cliques_list</span><span class="p">):</span>
            <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">            Finds the common cliques among the given set of cliques for</span>
<span class="sd">            corresponding node.</span>
<span class="sd">            &quot;&quot;&quot;</span>
            <span class="n">common</span> <span class="o">=</span> <span class="nb">set</span><span class="p">([</span><span class="nb">tuple</span><span class="p">(</span><span class="n">x</span><span class="p">)</span> <span class="k">for</span> <span class="n">x</span> <span class="ow">in</span> <span class="n">cliques_list</span><span class="p">[</span><span class="mi">0</span><span class="p">]])</span>
            <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="nb">len</span><span class="p">(</span><span class="n">cliques_list</span><span class="p">)):</span>
                <span class="n">common</span> <span class="o">=</span> <span class="n">common</span> <span class="o">&amp;</span> <span class="nb">set</span><span class="p">([</span><span class="nb">tuple</span><span class="p">(</span><span class="n">x</span><span class="p">)</span> <span class="k">for</span> <span class="n">x</span> <span class="ow">in</span> <span class="n">cliques_list</span><span class="p">[</span><span class="n">i</span><span class="p">]])</span>
            <span class="k">return</span> <span class="nb">list</span><span class="p">(</span><span class="n">common</span><span class="p">)</span>

        <span class="k">def</span> <span class="nf">_find_size_of_clique</span><span class="p">(</span><span class="n">clique</span><span class="p">,</span> <span class="n">cardinalities</span><span class="p">):</span>
            <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">            Computes the size of a clique.</span>

<span class="sd">            Size of a clique is defined as product of cardinalities of all the</span>
<span class="sd">            nodes present in the clique.</span>
<span class="sd">            &quot;&quot;&quot;</span>
            <span class="k">return</span> <span class="nb">list</span><span class="p">(</span><span class="nb">map</span><span class="p">(</span><span class="k">lambda</span> <span class="n">x</span><span class="p">:</span> <span class="n">np</span><span class="o">.</span><span class="n">prod</span><span class="p">([</span><span class="n">cardinalities</span><span class="p">[</span><span class="n">node</span><span class="p">]</span> <span class="k">for</span> <span class="n">node</span> <span class="ow">in</span> <span class="n">x</span><span class="p">]),</span>
                            <span class="n">clique</span><span class="p">))</span>

        <span class="k">def</span> <span class="nf">_get_cliques_dict</span><span class="p">(</span><span class="n">node</span><span class="p">):</span>
            <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">            Returns a dictionary in the form of {node: cliques_formed} of the</span>
<span class="sd">            node along with its neighboring nodes.</span>

<span class="sd">            clique_dict_removed would be containing the cliques created</span>
<span class="sd">            after deletion of the node</span>
<span class="sd">            clique_dict_node would be containing the cliques created before</span>
<span class="sd">            deletion of the node</span>
<span class="sd">            &quot;&quot;&quot;</span>
            <span class="n">graph_working_copy</span> <span class="o">=</span> <span class="n">nx</span><span class="o">.</span><span class="n">Graph</span><span class="p">(</span><span class="n">graph_copy</span><span class="o">.</span><span class="n">edges</span><span class="p">())</span>
            <span class="n">neighbors</span> <span class="o">=</span> <span class="n">graph_working_copy</span><span class="o">.</span><span class="n">neighbors</span><span class="p">(</span><span class="n">node</span><span class="p">)</span>
            <span class="n">graph_working_copy</span><span class="o">.</span><span class="n">add_edges_from</span><span class="p">(</span><span class="n">itertools</span><span class="o">.</span><span class="n">combinations</span><span class="p">(</span><span class="n">neighbors</span><span class="p">,</span> <span class="mi">2</span><span class="p">))</span>
            <span class="n">clique_dict</span> <span class="o">=</span> <span class="n">nx</span><span class="o">.</span><span class="n">cliques_containing_node</span><span class="p">(</span><span class="n">graph_working_copy</span><span class="p">,</span>
                                                     <span class="n">nodes</span><span class="o">=</span><span class="p">([</span><span class="n">node</span><span class="p">]</span> <span class="o">+</span> <span class="n">neighbors</span><span class="p">))</span>
            <span class="n">graph_working_copy</span><span class="o">.</span><span class="n">remove_node</span><span class="p">(</span><span class="n">node</span><span class="p">)</span>
            <span class="n">clique_dict_removed</span> <span class="o">=</span> <span class="n">nx</span><span class="o">.</span><span class="n">cliques_containing_node</span><span class="p">(</span><span class="n">graph_working_copy</span><span class="p">,</span>
                                                             <span class="n">nodes</span><span class="o">=</span><span class="n">neighbors</span><span class="p">)</span>
            <span class="k">return</span> <span class="n">clique_dict</span><span class="p">,</span> <span class="n">clique_dict_removed</span>

        <span class="k">if</span> <span class="ow">not</span> <span class="n">order</span><span class="p">:</span>
            <span class="n">order</span> <span class="o">=</span> <span class="p">[]</span>

            <span class="n">cardinalities</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_cardinality</span><span class="p">()</span>
            <span class="k">for</span> <span class="n">index</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">number_of_nodes</span><span class="p">()):</span>
                <span class="c1"># S represents the size of clique created by deleting the</span>
                <span class="c1"># node from the graph</span>
                <span class="n">S</span> <span class="o">=</span> <span class="p">{}</span>
                <span class="c1"># M represents the size of maximum size of cliques given by</span>
                <span class="c1"># the node and its adjacent node</span>
                <span class="n">M</span> <span class="o">=</span> <span class="p">{}</span>
                <span class="c1"># C represents the sum of size of the cliques created by the</span>
                <span class="c1"># node and its adjacent node</span>
                <span class="n">C</span> <span class="o">=</span> <span class="p">{}</span>
                <span class="k">for</span> <span class="n">node</span> <span class="ow">in</span> <span class="nb">set</span><span class="p">(</span><span class="n">graph_copy</span><span class="o">.</span><span class="n">nodes</span><span class="p">())</span> <span class="o">-</span> <span class="nb">set</span><span class="p">(</span><span class="n">order</span><span class="p">):</span>
                    <span class="n">clique_dict</span><span class="p">,</span> <span class="n">clique_dict_removed</span> <span class="o">=</span> <span class="n">_get_cliques_dict</span><span class="p">(</span><span class="n">node</span><span class="p">)</span>
                    <span class="n">S</span><span class="p">[</span><span class="n">node</span><span class="p">]</span> <span class="o">=</span> <span class="n">_find_size_of_clique</span><span class="p">(</span>
                        <span class="n">_find_common_cliques</span><span class="p">(</span><span class="nb">list</span><span class="p">(</span><span class="n">clique_dict_removed</span><span class="o">.</span><span class="n">values</span><span class="p">())),</span>
                        <span class="n">cardinalities</span>
                    <span class="p">)[</span><span class="mi">0</span><span class="p">]</span>
                    <span class="n">common_clique_size</span> <span class="o">=</span> <span class="n">_find_size_of_clique</span><span class="p">(</span>
                        <span class="n">_find_common_cliques</span><span class="p">(</span><span class="nb">list</span><span class="p">(</span><span class="n">clique_dict</span><span class="o">.</span><span class="n">values</span><span class="p">())),</span>
                        <span class="n">cardinalities</span>
                    <span class="p">)</span>
                    <span class="n">M</span><span class="p">[</span><span class="n">node</span><span class="p">]</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">max</span><span class="p">(</span><span class="n">common_clique_size</span><span class="p">)</span>
                    <span class="n">C</span><span class="p">[</span><span class="n">node</span><span class="p">]</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">sum</span><span class="p">(</span><span class="n">common_clique_size</span><span class="p">)</span>

                <span class="k">if</span> <span class="n">heuristic</span> <span class="o">==</span> <span class="s1">&#39;H1&#39;</span><span class="p">:</span>
                    <span class="n">node_to_delete</span> <span class="o">=</span> <span class="nb">min</span><span class="p">(</span><span class="n">S</span><span class="p">,</span> <span class="n">key</span><span class="o">=</span><span class="n">S</span><span class="o">.</span><span class="n">get</span><span class="p">)</span>

                <span class="k">elif</span> <span class="n">heuristic</span> <span class="o">==</span> <span class="s1">&#39;H2&#39;</span><span class="p">:</span>
                    <span class="n">S_by_E</span> <span class="o">=</span> <span class="p">{</span><span class="n">key</span><span class="p">:</span> <span class="n">S</span><span class="p">[</span><span class="n">key</span><span class="p">]</span> <span class="o">/</span> <span class="n">cardinalities</span><span class="p">[</span><span class="n">key</span><span class="p">]</span> <span class="k">for</span> <span class="n">key</span> <span class="ow">in</span> <span class="n">S</span><span class="p">}</span>
                    <span class="n">node_to_delete</span> <span class="o">=</span> <span class="nb">min</span><span class="p">(</span><span class="n">S_by_E</span><span class="p">,</span> <span class="n">key</span><span class="o">=</span><span class="n">S_by_E</span><span class="o">.</span><span class="n">get</span><span class="p">)</span>

                <span class="k">elif</span> <span class="n">heuristic</span> <span class="o">==</span> <span class="s1">&#39;H3&#39;</span><span class="p">:</span>
                    <span class="n">S_minus_M</span> <span class="o">=</span> <span class="p">{</span><span class="n">key</span><span class="p">:</span> <span class="n">S</span><span class="p">[</span><span class="n">key</span><span class="p">]</span> <span class="o">-</span> <span class="n">M</span><span class="p">[</span><span class="n">key</span><span class="p">]</span> <span class="k">for</span> <span class="n">key</span> <span class="ow">in</span> <span class="n">S</span><span class="p">}</span>
                    <span class="n">node_to_delete</span> <span class="o">=</span> <span class="nb">min</span><span class="p">(</span><span class="n">S_minus_M</span><span class="p">,</span> <span class="n">key</span><span class="o">=</span><span class="n">S_minus_M</span><span class="o">.</span><span class="n">get</span><span class="p">)</span>

                <span class="k">elif</span> <span class="n">heuristic</span> <span class="o">==</span> <span class="s1">&#39;H4&#39;</span><span class="p">:</span>
                    <span class="n">S_minus_C</span> <span class="o">=</span> <span class="p">{</span><span class="n">key</span><span class="p">:</span> <span class="n">S</span><span class="p">[</span><span class="n">key</span><span class="p">]</span> <span class="o">-</span> <span class="n">C</span><span class="p">[</span><span class="n">key</span><span class="p">]</span> <span class="k">for</span> <span class="n">key</span> <span class="ow">in</span> <span class="n">S</span><span class="p">}</span>
                    <span class="n">node_to_delete</span> <span class="o">=</span> <span class="nb">min</span><span class="p">(</span><span class="n">S_minus_C</span><span class="p">,</span> <span class="n">key</span><span class="o">=</span><span class="n">S_minus_C</span><span class="o">.</span><span class="n">get</span><span class="p">)</span>

                <span class="k">elif</span> <span class="n">heuristic</span> <span class="o">==</span> <span class="s1">&#39;H5&#39;</span><span class="p">:</span>
                    <span class="n">S_by_M</span> <span class="o">=</span> <span class="p">{</span><span class="n">key</span><span class="p">:</span> <span class="n">S</span><span class="p">[</span><span class="n">key</span><span class="p">]</span> <span class="o">/</span> <span class="n">M</span><span class="p">[</span><span class="n">key</span><span class="p">]</span> <span class="k">for</span> <span class="n">key</span> <span class="ow">in</span> <span class="n">S</span><span class="p">}</span>
                    <span class="n">node_to_delete</span> <span class="o">=</span> <span class="nb">min</span><span class="p">(</span><span class="n">S_by_M</span><span class="p">,</span> <span class="n">key</span><span class="o">=</span><span class="n">S_by_M</span><span class="o">.</span><span class="n">get</span><span class="p">)</span>

                <span class="k">else</span><span class="p">:</span>
                    <span class="n">S_by_C</span> <span class="o">=</span> <span class="p">{</span><span class="n">key</span><span class="p">:</span> <span class="n">S</span><span class="p">[</span><span class="n">key</span><span class="p">]</span> <span class="o">/</span> <span class="n">C</span><span class="p">[</span><span class="n">key</span><span class="p">]</span> <span class="k">for</span> <span class="n">key</span> <span class="ow">in</span> <span class="n">S</span><span class="p">}</span>
                    <span class="n">node_to_delete</span> <span class="o">=</span> <span class="nb">min</span><span class="p">(</span><span class="n">S_by_C</span><span class="p">,</span> <span class="n">key</span><span class="o">=</span><span class="n">S_by_C</span><span class="o">.</span><span class="n">get</span><span class="p">)</span>

                <span class="n">order</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">node_to_delete</span><span class="p">)</span>

        <span class="n">graph_copy</span> <span class="o">=</span> <span class="n">nx</span><span class="o">.</span><span class="n">Graph</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">edges</span><span class="p">())</span>
        <span class="k">for</span> <span class="n">node</span> <span class="ow">in</span> <span class="n">order</span><span class="p">:</span>
            <span class="k">for</span> <span class="n">edge</span> <span class="ow">in</span> <span class="n">itertools</span><span class="o">.</span><span class="n">combinations</span><span class="p">(</span><span class="n">graph_copy</span><span class="o">.</span><span class="n">neighbors</span><span class="p">(</span><span class="n">node</span><span class="p">),</span> <span class="mi">2</span><span class="p">):</span>
                <span class="n">graph_copy</span><span class="o">.</span><span class="n">add_edge</span><span class="p">(</span><span class="n">edge</span><span class="p">[</span><span class="mi">0</span><span class="p">],</span> <span class="n">edge</span><span class="p">[</span><span class="mi">1</span><span class="p">])</span>
                <span class="n">edge_set</span><span class="o">.</span><span class="n">add</span><span class="p">(</span><span class="n">edge</span><span class="p">)</span>
            <span class="n">graph_copy</span><span class="o">.</span><span class="n">remove_node</span><span class="p">(</span><span class="n">node</span><span class="p">)</span>

        <span class="k">if</span> <span class="n">inplace</span><span class="p">:</span>
            <span class="k">for</span> <span class="n">edge</span> <span class="ow">in</span> <span class="n">edge_set</span><span class="p">:</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">add_edge</span><span class="p">(</span><span class="n">edge</span><span class="p">[</span><span class="mi">0</span><span class="p">],</span> <span class="n">edge</span><span class="p">[</span><span class="mi">1</span><span class="p">])</span>
            <span class="k">return</span> <span class="bp">self</span>

        <span class="k">else</span><span class="p">:</span>
            <span class="n">graph_copy</span> <span class="o">=</span> <span class="n">MarkovModel</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">edges</span><span class="p">())</span>
            <span class="k">for</span> <span class="n">edge</span> <span class="ow">in</span> <span class="n">edge_set</span><span class="p">:</span>
                <span class="n">graph_copy</span><span class="o">.</span><span class="n">add_edge</span><span class="p">(</span><span class="n">edge</span><span class="p">[</span><span class="mi">0</span><span class="p">],</span> <span class="n">edge</span><span class="p">[</span><span class="mi">1</span><span class="p">])</span>
            <span class="k">return</span> <span class="n">graph_copy</span></div>

<div class="viewcode-block" id="MarkovModel.to_junction_tree"><a class="viewcode-back" href="../../../models.html#pgmpy.models.MarkovModel.MarkovModel.to_junction_tree">[docs]</a>    <span class="k">def</span> <span class="nf">to_junction_tree</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Creates a junction tree (or clique tree) for a given markov model.</span>

<span class="sd">        For a given markov model (H) a junction tree (G) is a graph</span>
<span class="sd">        1. where each node in G corresponds to a maximal clique in H</span>
<span class="sd">        2. each sepset in G separates the variables strictly on one side of the</span>
<span class="sd">        edge to other.</span>

<span class="sd">        Examples</span>
<span class="sd">        --------</span>
<span class="sd">        &gt;&gt;&gt; from pgmpy.models import MarkovModel</span>
<span class="sd">        &gt;&gt;&gt; from pgmpy.factors.discrete import DiscreteFactor</span>
<span class="sd">        &gt;&gt;&gt; mm = MarkovModel()</span>
<span class="sd">        &gt;&gt;&gt; mm.add_nodes_from([&#39;x1&#39;, &#39;x2&#39;, &#39;x3&#39;, &#39;x4&#39;, &#39;x5&#39;, &#39;x6&#39;, &#39;x7&#39;])</span>
<span class="sd">        &gt;&gt;&gt; mm.add_edges_from([(&#39;x1&#39;, &#39;x3&#39;), (&#39;x1&#39;, &#39;x4&#39;), (&#39;x2&#39;, &#39;x4&#39;),</span>
<span class="sd">        ...                    (&#39;x2&#39;, &#39;x5&#39;), (&#39;x3&#39;, &#39;x6&#39;), (&#39;x4&#39;, &#39;x6&#39;),</span>
<span class="sd">        ...                    (&#39;x4&#39;, &#39;x7&#39;), (&#39;x5&#39;, &#39;x7&#39;)])</span>
<span class="sd">        &gt;&gt;&gt; phi = [DiscreteFactor(edge, [2, 2], np.random.rand(4)) for edge in mm.edges()]</span>
<span class="sd">        &gt;&gt;&gt; mm.add_factors(*phi)</span>
<span class="sd">        &gt;&gt;&gt; junction_tree = mm.to_junction_tree()</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="kn">from</span> <span class="nn">pgmpy.models</span> <span class="k">import</span> <span class="n">JunctionTree</span>

        <span class="c1"># Check whether the model is valid or not</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">check_model</span><span class="p">()</span>

        <span class="c1"># Triangulate the graph to make it chordal</span>
        <span class="n">triangulated_graph</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">triangulate</span><span class="p">()</span>

        <span class="c1"># Find maximal cliques in the chordal graph</span>
        <span class="n">cliques</span> <span class="o">=</span> <span class="nb">list</span><span class="p">(</span><span class="nb">map</span><span class="p">(</span><span class="nb">tuple</span><span class="p">,</span> <span class="n">nx</span><span class="o">.</span><span class="n">find_cliques</span><span class="p">(</span><span class="n">triangulated_graph</span><span class="p">)))</span>

        <span class="c1"># If there is only 1 clique, then the junction tree formed is just a</span>
        <span class="c1"># clique tree with that single clique as the node</span>
        <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">cliques</span><span class="p">)</span> <span class="o">==</span> <span class="mi">1</span><span class="p">:</span>
            <span class="n">clique_trees</span> <span class="o">=</span> <span class="n">JunctionTree</span><span class="p">()</span>
            <span class="n">clique_trees</span><span class="o">.</span><span class="n">add_node</span><span class="p">(</span><span class="n">cliques</span><span class="p">[</span><span class="mi">0</span><span class="p">])</span>

        <span class="c1"># Else if the number of cliques is more than 1 then create a complete</span>
        <span class="c1"># graph with all the cliques as nodes and weight of the edges being</span>
        <span class="c1"># the length of sepset between two cliques</span>
        <span class="k">elif</span> <span class="nb">len</span><span class="p">(</span><span class="n">cliques</span><span class="p">)</span> <span class="o">&gt;=</span> <span class="mi">2</span><span class="p">:</span>
            <span class="n">complete_graph</span> <span class="o">=</span> <span class="n">UndirectedGraph</span><span class="p">()</span>
            <span class="n">edges</span> <span class="o">=</span> <span class="nb">list</span><span class="p">(</span><span class="n">itertools</span><span class="o">.</span><span class="n">combinations</span><span class="p">(</span><span class="n">cliques</span><span class="p">,</span> <span class="mi">2</span><span class="p">))</span>
            <span class="n">weights</span> <span class="o">=</span> <span class="nb">list</span><span class="p">(</span><span class="nb">map</span><span class="p">(</span><span class="k">lambda</span> <span class="n">x</span><span class="p">:</span> <span class="nb">len</span><span class="p">(</span><span class="nb">set</span><span class="p">(</span><span class="n">x</span><span class="p">[</span><span class="mi">0</span><span class="p">])</span><span class="o">.</span><span class="n">intersection</span><span class="p">(</span><span class="nb">set</span><span class="p">(</span><span class="n">x</span><span class="p">[</span><span class="mi">1</span><span class="p">]))),</span>
                           <span class="n">edges</span><span class="p">))</span>
            <span class="k">for</span> <span class="n">edge</span><span class="p">,</span> <span class="n">weight</span> <span class="ow">in</span> <span class="nb">zip</span><span class="p">(</span><span class="n">edges</span><span class="p">,</span> <span class="n">weights</span><span class="p">):</span>
                <span class="n">complete_graph</span><span class="o">.</span><span class="n">add_edge</span><span class="p">(</span><span class="o">*</span><span class="n">edge</span><span class="p">,</span> <span class="n">weight</span><span class="o">=-</span><span class="n">weight</span><span class="p">)</span>

            <span class="c1"># Create clique trees by minimum (or maximum) spanning tree method</span>
            <span class="n">clique_trees</span> <span class="o">=</span> <span class="n">JunctionTree</span><span class="p">(</span><span class="n">nx</span><span class="o">.</span><span class="n">minimum_spanning_tree</span><span class="p">(</span><span class="n">complete_graph</span><span class="p">)</span><span class="o">.</span><span class="n">edges</span><span class="p">())</span>

        <span class="c1"># Check whether the factors are defined for all the random variables or not</span>
        <span class="n">all_vars</span> <span class="o">=</span> <span class="n">itertools</span><span class="o">.</span><span class="n">chain</span><span class="p">(</span><span class="o">*</span><span class="p">[</span><span class="n">factor</span><span class="o">.</span><span class="n">scope</span><span class="p">()</span> <span class="k">for</span> <span class="n">factor</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">factors</span><span class="p">])</span>
        <span class="k">if</span> <span class="nb">set</span><span class="p">(</span><span class="n">all_vars</span><span class="p">)</span> <span class="o">!=</span> <span class="nb">set</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">nodes</span><span class="p">()):</span>
            <span class="ne">ValueError</span><span class="p">(</span><span class="s1">&#39;DiscreteFactor for all the random variables not specified&#39;</span><span class="p">)</span>

        <span class="c1"># Dictionary stating whether the factor is used to create clique</span>
        <span class="c1"># potential or not</span>
        <span class="c1"># If false, then it is not used to create any clique potential</span>
        <span class="n">is_used</span> <span class="o">=</span> <span class="p">{</span><span class="n">factor</span><span class="p">:</span> <span class="kc">False</span> <span class="k">for</span> <span class="n">factor</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">factors</span><span class="p">}</span>

        <span class="k">for</span> <span class="n">node</span> <span class="ow">in</span> <span class="n">clique_trees</span><span class="o">.</span><span class="n">nodes</span><span class="p">():</span>
            <span class="n">clique_factors</span> <span class="o">=</span> <span class="p">[]</span>
            <span class="k">for</span> <span class="n">factor</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">factors</span><span class="p">:</span>
                <span class="c1"># If the factor is not used in creating any clique potential as</span>
                <span class="c1"># well as has any variable of the given clique in its scope,</span>
                <span class="c1"># then use it in creating clique potential</span>
                <span class="k">if</span> <span class="ow">not</span> <span class="n">is_used</span><span class="p">[</span><span class="n">factor</span><span class="p">]</span> <span class="ow">and</span> <span class="nb">set</span><span class="p">(</span><span class="n">factor</span><span class="o">.</span><span class="n">scope</span><span class="p">())</span><span class="o">.</span><span class="n">issubset</span><span class="p">(</span><span class="n">node</span><span class="p">):</span>
                    <span class="n">clique_factors</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">factor</span><span class="p">)</span>
                    <span class="n">is_used</span><span class="p">[</span><span class="n">factor</span><span class="p">]</span> <span class="o">=</span> <span class="kc">True</span>

            <span class="c1"># To compute clique potential, initially set it as unity factor</span>
            <span class="n">var_card</span> <span class="o">=</span> <span class="p">[</span><span class="bp">self</span><span class="o">.</span><span class="n">get_cardinality</span><span class="p">()[</span><span class="n">x</span><span class="p">]</span> <span class="k">for</span> <span class="n">x</span> <span class="ow">in</span> <span class="n">node</span><span class="p">]</span>
            <span class="n">clique_potential</span> <span class="o">=</span> <span class="n">DiscreteFactor</span><span class="p">(</span><span class="n">node</span><span class="p">,</span> <span class="n">var_card</span><span class="p">,</span> <span class="n">np</span><span class="o">.</span><span class="n">ones</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">product</span><span class="p">(</span><span class="n">var_card</span><span class="p">)))</span>
            <span class="c1"># multiply it with the factors associated with the variables present</span>
            <span class="c1"># in the clique (or node)</span>
            <span class="n">clique_potential</span> <span class="o">*=</span> <span class="n">factor_product</span><span class="p">(</span><span class="o">*</span><span class="n">clique_factors</span><span class="p">)</span>
            <span class="n">clique_trees</span><span class="o">.</span><span class="n">add_factors</span><span class="p">(</span><span class="n">clique_potential</span><span class="p">)</span>

        <span class="k">if</span> <span class="ow">not</span> <span class="nb">all</span><span class="p">(</span><span class="n">is_used</span><span class="o">.</span><span class="n">values</span><span class="p">()):</span>
            <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s1">&#39;All the factors were not used to create Junction Tree.&#39;</span>
                             <span class="s1">&#39;Extra factors are defined.&#39;</span><span class="p">)</span>

        <span class="k">return</span> <span class="n">clique_trees</span></div>

<div class="viewcode-block" id="MarkovModel.markov_blanket"><a class="viewcode-back" href="../../../models.html#pgmpy.models.MarkovModel.MarkovModel.markov_blanket">[docs]</a>    <span class="k">def</span> <span class="nf">markov_blanket</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">node</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Returns a markov blanket for a random variable.</span>

<span class="sd">        Markov blanket is the neighboring nodes of the given node.</span>

<span class="sd">        Examples</span>
<span class="sd">        --------</span>
<span class="sd">        &gt;&gt;&gt; from pgmpy.models import MarkovModel</span>
<span class="sd">        &gt;&gt;&gt; mm = MarkovModel()</span>
<span class="sd">        &gt;&gt;&gt; mm.add_nodes_from([&#39;x1&#39;, &#39;x2&#39;, &#39;x3&#39;, &#39;x4&#39;, &#39;x5&#39;, &#39;x6&#39;, &#39;x7&#39;])</span>
<span class="sd">        &gt;&gt;&gt; mm.add_edges_from([(&#39;x1&#39;, &#39;x3&#39;), (&#39;x1&#39;, &#39;x4&#39;), (&#39;x2&#39;, &#39;x4&#39;),</span>
<span class="sd">        ...                    (&#39;x2&#39;, &#39;x5&#39;), (&#39;x3&#39;, &#39;x6&#39;), (&#39;x4&#39;, &#39;x6&#39;),</span>
<span class="sd">        ...                    (&#39;x4&#39;, &#39;x7&#39;), (&#39;x5&#39;, &#39;x7&#39;)])</span>
<span class="sd">        &gt;&gt;&gt; mm.markov_blanket(&#39;x1&#39;)</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">neighbors</span><span class="p">(</span><span class="n">node</span><span class="p">)</span></div>

<div class="viewcode-block" id="MarkovModel.get_local_independencies"><a class="viewcode-back" href="../../../models.html#pgmpy.models.MarkovModel.MarkovModel.get_local_independencies">[docs]</a>    <span class="k">def</span> <span class="nf">get_local_independencies</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">latex</span><span class="o">=</span><span class="kc">False</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Returns all the local independencies present in the markov model.</span>

<span class="sd">        Local independencies are the independence assertion in the form of</span>
<span class="sd">        .. math:: {X \perp W - {X} - MB(X) | MB(X)}</span>
<span class="sd">        where MB is the markov blanket of all the random variables in X</span>

<span class="sd">        Parameters</span>
<span class="sd">        ----------</span>
<span class="sd">        latex: boolean</span>
<span class="sd">            If latex=True then latex string of the indepedence assertion would</span>
<span class="sd">            be created</span>

<span class="sd">        Examples</span>
<span class="sd">        --------</span>
<span class="sd">        &gt;&gt;&gt; from pgmpy.models import MarkovModel</span>
<span class="sd">        &gt;&gt;&gt; mm = MarkovModel()</span>
<span class="sd">        &gt;&gt;&gt; mm.add_nodes_from([&#39;x1&#39;, &#39;x2&#39;, &#39;x3&#39;, &#39;x4&#39;, &#39;x5&#39;, &#39;x6&#39;, &#39;x7&#39;])</span>
<span class="sd">        &gt;&gt;&gt; mm.add_edges_from([(&#39;x1&#39;, &#39;x3&#39;), (&#39;x1&#39;, &#39;x4&#39;), (&#39;x2&#39;, &#39;x4&#39;),</span>
<span class="sd">        ...                    (&#39;x2&#39;, &#39;x5&#39;), (&#39;x3&#39;, &#39;x6&#39;), (&#39;x4&#39;, &#39;x6&#39;),</span>
<span class="sd">        ...                    (&#39;x4&#39;, &#39;x7&#39;), (&#39;x5&#39;, &#39;x7&#39;)])</span>
<span class="sd">        &gt;&gt;&gt; mm.get_local_independecies()</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">local_independencies</span> <span class="o">=</span> <span class="n">Independencies</span><span class="p">()</span>

        <span class="n">all_vars</span> <span class="o">=</span> <span class="nb">set</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">nodes</span><span class="p">())</span>
        <span class="k">for</span> <span class="n">node</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">nodes</span><span class="p">():</span>
            <span class="n">markov_blanket</span> <span class="o">=</span> <span class="nb">set</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">markov_blanket</span><span class="p">(</span><span class="n">node</span><span class="p">))</span>
            <span class="n">rest</span> <span class="o">=</span> <span class="n">all_vars</span> <span class="o">-</span> <span class="nb">set</span><span class="p">([</span><span class="n">node</span><span class="p">])</span> <span class="o">-</span> <span class="n">markov_blanket</span>
            <span class="k">try</span><span class="p">:</span>
                <span class="n">local_independencies</span><span class="o">.</span><span class="n">add_assertions</span><span class="p">([</span><span class="n">node</span><span class="p">,</span> <span class="nb">list</span><span class="p">(</span><span class="n">rest</span><span class="p">),</span> <span class="nb">list</span><span class="p">(</span><span class="n">markov_blanket</span><span class="p">)])</span>
            <span class="k">except</span> <span class="ne">ValueError</span><span class="p">:</span>
                <span class="k">pass</span>

        <span class="n">local_independencies</span><span class="o">.</span><span class="n">reduce</span><span class="p">()</span>

        <span class="k">if</span> <span class="n">latex</span><span class="p">:</span>
            <span class="k">return</span> <span class="n">local_independencies</span><span class="o">.</span><span class="n">latex_string</span><span class="p">()</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="k">return</span> <span class="n">local_independencies</span></div>

<div class="viewcode-block" id="MarkovModel.to_bayesian_model"><a class="viewcode-back" href="../../../models.html#pgmpy.models.MarkovModel.MarkovModel.to_bayesian_model">[docs]</a>    <span class="k">def</span> <span class="nf">to_bayesian_model</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Creates a Bayesian Model which is a minimum I-Map for this markov model.</span>

<span class="sd">        The ordering of parents may not remain constant. It would depend on the</span>
<span class="sd">        ordering of variable in the junction tree (which is not constant) all the</span>
<span class="sd">        time.</span>

<span class="sd">        Examples</span>
<span class="sd">        --------</span>
<span class="sd">        &gt;&gt;&gt; from pgmpy.models import MarkovModel</span>
<span class="sd">        &gt;&gt;&gt; from pgmpy.factors.discrete import DiscreteFactor</span>
<span class="sd">        &gt;&gt;&gt; mm = MarkovModel()</span>
<span class="sd">        &gt;&gt;&gt; mm.add_nodes_from([&#39;x1&#39;, &#39;x2&#39;, &#39;x3&#39;, &#39;x4&#39;, &#39;x5&#39;, &#39;x6&#39;, &#39;x7&#39;])</span>
<span class="sd">        &gt;&gt;&gt; mm.add_edges_from([(&#39;x1&#39;, &#39;x3&#39;), (&#39;x1&#39;, &#39;x4&#39;), (&#39;x2&#39;, &#39;x4&#39;),</span>
<span class="sd">        ...                    (&#39;x2&#39;, &#39;x5&#39;), (&#39;x3&#39;, &#39;x6&#39;), (&#39;x4&#39;, &#39;x6&#39;),</span>
<span class="sd">        ...                    (&#39;x4&#39;, &#39;x7&#39;), (&#39;x5&#39;, &#39;x7&#39;)])</span>
<span class="sd">        &gt;&gt;&gt; phi = [DiscreteFactor(edge, [2, 2], np.random.rand(4)) for edge in mm.edges()]</span>
<span class="sd">        &gt;&gt;&gt; mm.add_factors(*phi)</span>
<span class="sd">        &gt;&gt;&gt; bm = mm.to_bayesian_model()</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="kn">from</span> <span class="nn">pgmpy.models</span> <span class="k">import</span> <span class="n">BayesianModel</span>

        <span class="n">bm</span> <span class="o">=</span> <span class="n">BayesianModel</span><span class="p">()</span>
        <span class="n">var_clique_dict</span> <span class="o">=</span> <span class="n">defaultdict</span><span class="p">(</span><span class="nb">tuple</span><span class="p">)</span>
        <span class="n">var_order</span> <span class="o">=</span> <span class="p">[]</span>

        <span class="c1"># Create a junction tree from the markov model.</span>
        <span class="c1"># Creation of clique tree involves triangulation, finding maximal cliques</span>
        <span class="c1"># and creating a tree from these cliques</span>
        <span class="n">junction_tree</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">to_junction_tree</span><span class="p">()</span>

        <span class="c1"># create an ordering of the nodes based on the ordering of the clique</span>
        <span class="c1"># in which it appeared first</span>
        <span class="n">root_node</span> <span class="o">=</span> <span class="n">junction_tree</span><span class="o">.</span><span class="n">nodes</span><span class="p">()[</span><span class="mi">0</span><span class="p">]</span>
        <span class="n">bfs_edges</span> <span class="o">=</span> <span class="n">nx</span><span class="o">.</span><span class="n">bfs_edges</span><span class="p">(</span><span class="n">junction_tree</span><span class="p">,</span> <span class="n">root_node</span><span class="p">)</span>
        <span class="k">for</span> <span class="n">node</span> <span class="ow">in</span> <span class="n">root_node</span><span class="p">:</span>
            <span class="n">var_clique_dict</span><span class="p">[</span><span class="n">node</span><span class="p">]</span> <span class="o">=</span> <span class="n">root_node</span>
            <span class="n">var_order</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">node</span><span class="p">)</span>
        <span class="k">for</span> <span class="n">edge</span> <span class="ow">in</span> <span class="n">bfs_edges</span><span class="p">:</span>
            <span class="n">clique_node</span> <span class="o">=</span> <span class="n">edge</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span>
            <span class="k">for</span> <span class="n">node</span> <span class="ow">in</span> <span class="n">clique_node</span><span class="p">:</span>
                <span class="k">if</span> <span class="ow">not</span> <span class="n">var_clique_dict</span><span class="p">[</span><span class="n">node</span><span class="p">]:</span>
                    <span class="n">var_clique_dict</span><span class="p">[</span><span class="n">node</span><span class="p">]</span> <span class="o">=</span> <span class="n">clique_node</span>
                    <span class="n">var_order</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">node</span><span class="p">)</span>

        <span class="c1"># create a bayesian model by adding edges from parent of node to node as</span>
        <span class="c1"># par(x_i) = (var(c_k) - x_i) \cap {x_1, ..., x_{i-1}}</span>
        <span class="k">for</span> <span class="n">node_index</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="nb">len</span><span class="p">(</span><span class="n">var_order</span><span class="p">)):</span>
            <span class="n">node</span> <span class="o">=</span> <span class="n">var_order</span><span class="p">[</span><span class="n">node_index</span><span class="p">]</span>
            <span class="n">node_parents</span> <span class="o">=</span> <span class="p">(</span><span class="nb">set</span><span class="p">(</span><span class="n">var_clique_dict</span><span class="p">[</span><span class="n">node</span><span class="p">])</span> <span class="o">-</span> <span class="nb">set</span><span class="p">([</span><span class="n">node</span><span class="p">]))</span><span class="o">.</span><span class="n">intersection</span><span class="p">(</span>
                <span class="nb">set</span><span class="p">(</span><span class="n">var_order</span><span class="p">[:</span><span class="n">node_index</span><span class="p">]))</span>
            <span class="n">bm</span><span class="o">.</span><span class="n">add_edges_from</span><span class="p">([(</span><span class="n">parent</span><span class="p">,</span> <span class="n">node</span><span class="p">)</span> <span class="k">for</span> <span class="n">parent</span> <span class="ow">in</span> <span class="n">node_parents</span><span class="p">])</span>
            <span class="c1"># TODO : Convert factor into CPDs</span>
        <span class="k">return</span> <span class="n">bm</span></div>

<div class="viewcode-block" id="MarkovModel.get_partition_function"><a class="viewcode-back" href="../../../models.html#pgmpy.models.MarkovModel.MarkovModel.get_partition_function">[docs]</a>    <span class="k">def</span> <span class="nf">get_partition_function</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Returns the partition function for a given undirected graph.</span>

<span class="sd">        A partition function is defined as</span>

<span class="sd">        .. math:: \sum_{X}(\prod_{i=1}^{m} \phi_i)</span>

<span class="sd">        where m is the number of factors present in the graph</span>
<span class="sd">        and X are all the random variables present.</span>

<span class="sd">        Examples</span>
<span class="sd">        --------</span>
<span class="sd">        &gt;&gt;&gt; from pgmpy.models import MarkovModel</span>
<span class="sd">        &gt;&gt;&gt; from pgmpy.factors.discrete import DiscreteFactor</span>
<span class="sd">        &gt;&gt;&gt; G = MarkovModel()</span>
<span class="sd">        &gt;&gt;&gt; G.add_nodes_from([&#39;x1&#39;, &#39;x2&#39;, &#39;x3&#39;, &#39;x4&#39;, &#39;x5&#39;, &#39;x6&#39;, &#39;x7&#39;])</span>
<span class="sd">        &gt;&gt;&gt; G.add_edges_from([(&#39;x1&#39;, &#39;x3&#39;), (&#39;x1&#39;, &#39;x4&#39;), (&#39;x2&#39;, &#39;x4&#39;),</span>
<span class="sd">        ...                   (&#39;x2&#39;, &#39;x5&#39;), (&#39;x3&#39;, &#39;x6&#39;), (&#39;x4&#39;, &#39;x6&#39;),</span>
<span class="sd">        ...                   (&#39;x4&#39;, &#39;x7&#39;), (&#39;x5&#39;, &#39;x7&#39;)])</span>
<span class="sd">        &gt;&gt;&gt; phi = [DiscreteFactor(edge, [2, 2], np.random.rand(4)) for edge in G.edges()]</span>
<span class="sd">        &gt;&gt;&gt; G.add_factors(*phi)</span>
<span class="sd">        &gt;&gt;&gt; G.get_partition_function()</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">check_model</span><span class="p">()</span>

        <span class="n">factor</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">factors</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span>
        <span class="n">factor</span> <span class="o">=</span> <span class="n">factor_product</span><span class="p">(</span><span class="n">factor</span><span class="p">,</span> <span class="o">*</span><span class="p">[</span><span class="bp">self</span><span class="o">.</span><span class="n">factors</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span>
                                          <span class="nb">range</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="nb">len</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">factors</span><span class="p">))])</span>
        <span class="k">if</span> <span class="nb">set</span><span class="p">(</span><span class="n">factor</span><span class="o">.</span><span class="n">scope</span><span class="p">())</span> <span class="o">!=</span> <span class="nb">set</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">nodes</span><span class="p">()):</span>
            <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s1">&#39;DiscreteFactor for all the random variables not defined.&#39;</span><span class="p">)</span>

        <span class="k">return</span> <span class="n">np</span><span class="o">.</span><span class="n">sum</span><span class="p">(</span><span class="n">factor</span><span class="o">.</span><span class="n">values</span><span class="p">)</span></div>

<div class="viewcode-block" id="MarkovModel.copy"><a class="viewcode-back" href="../../../models.html#pgmpy.models.MarkovModel.MarkovModel.copy">[docs]</a>    <span class="k">def</span> <span class="nf">copy</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Returns a copy of this Markov Model.</span>

<span class="sd">        Returns</span>
<span class="sd">        -------</span>
<span class="sd">        MarkovModel: Copy of this Markov model.</span>

<span class="sd">        Examples</span>
<span class="sd">        -------</span>
<span class="sd">        &gt;&gt;&gt; from pgmpy.factors.discrete import DiscreteFactor</span>
<span class="sd">        &gt;&gt;&gt; from pgmpy.models import MarkovModel</span>
<span class="sd">        &gt;&gt;&gt; G = MarkovModel()</span>
<span class="sd">        &gt;&gt;&gt; G.add_nodes_from([(&#39;a&#39;, &#39;b&#39;), (&#39;b&#39;, &#39;c&#39;)])</span>
<span class="sd">        &gt;&gt;&gt; G.add_edge((&#39;a&#39;, &#39;b&#39;), (&#39;b&#39;, &#39;c&#39;))</span>
<span class="sd">        &gt;&gt;&gt; G_copy = G.copy()</span>
<span class="sd">        &gt;&gt;&gt; G_copy.edges()</span>
<span class="sd">        [((&#39;a&#39;, &#39;b&#39;), (&#39;b&#39;, &#39;c&#39;))]</span>
<span class="sd">        &gt;&gt;&gt; G_copy.nodes()</span>
<span class="sd">        [(&#39;a&#39;, &#39;b&#39;), (&#39;b&#39;, &#39;c&#39;)]</span>
<span class="sd">        &gt;&gt;&gt; factor = DiscreteFactor([(&#39;a&#39;, &#39;b&#39;)], cardinality=[3],</span>
<span class="sd">        ...                 values=np.random.rand(3))</span>
<span class="sd">        &gt;&gt;&gt; G.add_factors(factor)</span>
<span class="sd">        &gt;&gt;&gt; G.get_factors()</span>
<span class="sd">        [&lt;DiscreteFactor representing phi((&#39;a&#39;, &#39;b&#39;):3) at 0x...&gt;]</span>
<span class="sd">        &gt;&gt;&gt; G_copy.get_factors()</span>
<span class="sd">        []</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">clone_graph</span> <span class="o">=</span> <span class="n">MarkovModel</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">edges</span><span class="p">())</span>
        <span class="n">clone_graph</span><span class="o">.</span><span class="n">add_nodes_from</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">nodes</span><span class="p">())</span>

        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">factors</span><span class="p">:</span>
            <span class="n">factors_copy</span> <span class="o">=</span> <span class="p">[</span><span class="n">factor</span><span class="o">.</span><span class="n">copy</span><span class="p">()</span> <span class="k">for</span> <span class="n">factor</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">factors</span><span class="p">]</span>
            <span class="n">clone_graph</span><span class="o">.</span><span class="n">add_factors</span><span class="p">(</span><span class="o">*</span><span class="n">factors_copy</span><span class="p">)</span>

        <span class="k">return</span> <span class="n">clone_graph</span></div></div>
</pre></div>

          </div>
        </div>
      </div>
      <div class="clearer"></div>
    </div>
    <div class="related" role="navigation" aria-label="related navigation">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../../../genindex.html" title="General Index"
             >index</a></li>
        <li class="right" >
          <a href="../../../py-modindex.html" title="Python Module Index"
             >modules</a> |</li>
        <li class="nav-item nav-item-0"><a href="../../../index.html">pgmpy 0.1.2 documentation</a> &#187;</li>
          <li class="nav-item nav-item-1"><a href="../../index.html" >Module code</a> &#187;</li> 
      </ul>
    </div>
    <div class="footer" role="contentinfo">
        &#169; Copyright 2016, Ankur Ankan.
      Created using <a href="http://sphinx-doc.org/">Sphinx</a> 1.5.1.
    </div>
  </body>
</html>